package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"video/models"
)

type VideoController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *VideoController) Add() {
	title := c.GetString("title")
	content := c.GetString("content")
	videoImg := c.GetString("videoImg")
	cate, _ := c.GetInt("cate")
	video := models.Video{
		Title:    title,
		Content:  content,
		VideoImg: videoImg,
		Cate:     uint(cate),
	}
	err := models.DB.Create(&video).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: video,
		}
		c.ServeJSON()
	}
}

func (c *VideoController) Upload() {
	file, header, err := c.GetFile("videoImg")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "请选择文件并上传",
			Data: nil,
		}
		c.ServeJSON()
	}

	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "视频大小不能大于2MB",
			Data: nil,
		}
		c.ServeJSON()
	}

	path := "static/video/" + header.Filename

	err = c.SaveToFile("videoImg", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		video := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "上传成功",
			Data: video,
		}
		c.ServeJSON()
	}
}

func (c *VideoController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	pageData := make(map[string]interface{})
	query := models.DB.Model(&models.Video{})
	key := "PAGE-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		query.Count(&count)
		offset := (page - 1) * size
		var videos []models.Video
		err = models.DB.Model(&models.Video{}).Offset(offset).Limit(size).Find(&videos).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			pageData["count"] = count
			pageData["videos"] = videos
			jsonByte, _ := json.Marshal(pageData)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Second*5)

			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}

func (c *VideoController) Info() {
	id, _ := c.GetInt("id")

	key := "PAGE-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()

	var question models.Video
	if err != nil {
		err = models.DB.Model(&models.Video{}).Where("ID=?", id).First(&question).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "详情展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&question)
			value := string(marshal)
			models.Rdb.Set(key, value, time.Second*60) //存储缓存
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情展示成功-mysql",
				Data: question,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &question)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "详情展示成功-redis",
			Data: question,
		}
		c.ServeJSON()
	}

}
